/*! Bezier Easing ~ Sean Catchpole - Copyright 2010 MIT/GPL */
(function($){ if(!$){ return }

  var midpoint = function(t,a,b){
    return { x: t*(b.x-a.x) + a.x
           , y: t*(b.y-a.y) + a.y }
  };

  var bezier = function(t,p){
    p=p.slice();
    p.unshift({x:0,y:0});
    p.push({x:1,y:1});
    var i, n = p.length;
    while( n-->1 ){
      for( i=0; i<n; i++ ){
        p[i] = midpoint( t, p[i], p[i+1] );
      }
    }
    return p[0];
  };


  $.bezier = function(){
    var args = [].slice.call(arguments)
      , list=[], p, last=0, N=$.bezier.samples;
    for(i=N; i-->0;){
      p = bezier(i/N,args);
      list[Math.round(p.x*N)] = p.y;
    }
    for(i=0; i<=N; i++){
      if(list[i]==null){ list[i]=last; }
      else{ last=list[i]; }
    }
    return function(t,n,a,b){
      n = Math.floor(t*N);
      n = !n?0:n<N?list[n]+(list[n+1]-list[n])*(t*N-n):1;
      return n*(b-a) + a;
    };
  };
  $.bezier.samples = 100;

  $.easing["ease"] = $.bezier( {x:0.25,y:0.1}, {x:0.25,y:1} );
  $.easing["ease-in"] = $.bezier( {x:0.42,y:0}, {x:1,y:1} );
  $.easing["ease-out"] = $.bezier( {x:0,y:0}, {x:0.58,y:1} );
  $.easing["ease-in-out"] = $.bezier( {x:0.42,y:0}, {x:0.58,y:1} );

})(jQuery);
